package com.yang.leather.common.page;

/**
 * 简单分页类
 * 
 */
public class SimplePage implements Paginable {
	private static final long serialVersionUID = 1L;
	public static final int DEF_COUNT = 20;
	public static final int maxShowPage = 10;//分页条最多显示10个分页数字

	/**
	 * 检查页码 checkPageNo
	 * 
	 * @param pageNo
	 * @return if pageNo==null or pageNo<1 then return 1 else return pageNo
	 */
	public static int cpn(Integer pageNo) {
		return (pageNo == null || pageNo < 1) ? 1 : pageNo;
	}

	public SimplePage() {
	}

	/**
	 * 构造器
	 * 
	 * @param pageNo
	 *            页码
	 * @param pageSize
	 *            每页几条数据
	 * @param totalCount
	 *            总共几条数据
	 */
	public SimplePage(int pageNo, int pageSize, int totalCount) {
		setTotalCount(totalCount);
		setPageSize(pageSize);
		setPageNo(pageNo);
		
		int totalPages = totalCount / pageSize;
		if (totalPages == 0 || totalCount % pageSize != 0) {
			totalPages++;
		}
		setTotalPage(totalPages);
		
		adjustPageNo();
		
		int nextPage = getNextPage();
		int prevPage = getPrePage();
		if(nextPage>pageNo){
			this.setHasNext(true);
		}
		if(prevPage<pageNo){
			this.setHasPrev(true);
		}
		
		//当显示页数大于定义的最大显示数字数时，需确定分页条起始显示数字
		if(this.totalPage>maxShowPage){
			
			if(this.pageNo+4<=maxShowPage){
				this.startPage = 1;
			}else{
				this.startPage = this.pageNo-2;
			}
			
			if(this.totalPage>this.pageNo+4){
				this.endPage = this.pageNo+4;
			}else{
				this.startPage = this.totalPage-6;
				this.endPage = this.totalPage;
			}
		}
	}

	/**
	 * 调整页码，使不超过最大页数
	 */
	public void adjustPageNo() {
		if (this.pageNo == 1) {
			return;
		}
		int tp = getTotalPage();
		if (this.pageNo > tp) {
			this.pageNo = tp;
		}
	}

	/**
	 * 获得页码
	 */
	public int getPageNo() {
		return this.pageNo;
	}

	/**
	 * 每页几条数据
	 */
	public int getPageSize() {
		return this.pageSize;
	}

	/**
	 * 总共几条数据
	 */
	public int getTotalCount() {
		return this.totalCount;
	}

	/**
	 * 总共几页
	 */
	public int getTotalPage() {
		return this.totalPage;
	}

	/**
	 * 是否第一页
	 */
	public boolean isFirstPage() {
		return this.pageNo <= 1;
	}

	/**
	 * 是否最后一页
	 */
	public boolean isLastPage() {
		return this.pageNo >= getTotalPage();
	}

	/**
	 * 下一页页码
	 */
	public int getNextPage() {
		if (isLastPage()) {
			return this.pageNo;
		} else {
			return this.pageNo + 1;
		}
	}

	/**
	 * 上一页页码
	 */
	public int getPrePage() {
		if (isFirstPage()) {
			return this.pageNo;
		} else {
			return this.pageNo - 1;
		}
	}

	protected int totalCount = 0;
	protected int pageSize = 20;
	protected int pageNo = 1;
	protected int totalPage = 0;
	protected boolean hasPrev = false;
	protected boolean hasNext = false;
	protected int startPage = 0;
	protected int endPage = 0;

	/**
	 * if totalCount<0 then totalCount=0
	 * 
	 * @param totalCount
	 */
	public void setTotalCount(int totalCount) {
		if (totalCount < 0) {
			this.totalCount = 0;
		} else {
			this.totalCount = totalCount;
		}
	}

	/**
	 * if pageSize< 1 then pageSize=DEF_COUNT
	 * 
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		if (pageSize < 1) {
			this.pageSize = DEF_COUNT;
		} else {
			this.pageSize = pageSize;
		}
	}

	/**
	 * if pageNo < 1 then pageNo=1
	 * 
	 * @param pageNo
	 */
	public void setPageNo(int pageNo) {
		if (pageNo < 1) {
			this.pageNo = 1;
		} else {
			this.pageNo = pageNo;
		}
	}

	public boolean isHasPrev() {
		return this.hasPrev;
	}

	public void setHasPrev(boolean hasPrev) {
		this.hasPrev = hasPrev;
	}

	public boolean isHasNext() {
		return this.hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	
}
